package com.yundingai.common;

import com.yundingai.domain.vo.RoomChatVo;
import com.yundingai.domain.vo.RoomSdpVo;
import com.yundingai.service.RoomChatService;
import com.yundingai.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author zhaoj
 * @Date 2025/7/19 14:30
 * @Description 两个监听器分别监听chat消息和sdp消息
 **/
@Slf4j
@Component
public class RabbitListener {
    @Autowired
    RoomChatService roomChatService;

    @Autowired
    RoomService roomService;

    /**
     * @Author zhaoj
     * @Date 2025/7/24 16:52
     * @Description sdp消息处理，用户join或exit时，更新roomList与room-memberlist
     **/
    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "sdp_queue")
    //这里如果收到信息类型不正确将导致后台崩溃
    public void receiveSDP(RoomSdpVo message){
        log.info("收到 SDP 消息, 类型:{}, 来自:{}",message.getType(),message.getFromUserId());
        if (Objects.equals(message.getType(), "heartbeat")){
            roomService.handleHeartBeat(message);
        }else if (Objects.equals(message.getType(), "join")){
            roomService.handleJoin(message);
        }
        //在线状态改用心跳检测这里会导致重复判断
//        else if (Objects.equals(message.getType(), "exit")){
//            roomService.handleExit(message);
//        }
    }
    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "chat_queue")
    public void receiveChat(RoomChatVo roomChatVo){
        log.info("收到 聊天 消息:{}",roomChatVo.toString());
        // 处理聊天消息
        roomChatService.handleChatMessage(roomChatVo);
    }
}
